package com.feicheng.iou.service.impl.statisticsInfo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.iou.entity.*;
import com.feicheng.iou.mapper.*;
import com.feicheng.iou.pojo.statisticsInfoPOJO.*;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.statisticsInfo.StatisticsInfoService;
import com.feicheng.iou.util.PageUtil;
import com.feicheng.iou.util.PermissionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.Soundbank;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * <p>
 * 统计业务实现类
 * </p>
 *
 * @author wuweinan
 * @since 2021-8-9
 */
@Service
@Slf4j
public class StatisticsInfoServiceImpl implements StatisticsInfoService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private InterviewMapper interviewMapper;

    @Autowired
    private TryMapper tryMapper;

    @Autowired
    private ReservationBackviewMapper reservationBackviewMapper;

    @Autowired
    private BackviewMapper backviewMapper;

    @Autowired
    private ReservationInterviewMapper reservationInterviewMapper;

    @Autowired
    private ReservationTryMapper reservationTryMapper;

    @Autowired
    private BaomingMapper baomingMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计用户效绩所有信息
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsAllUser(StatisticsInfoParamPOJO statisticsInfoParamPOJO) {
        log.info("StatisticsInfoServiceImpl的statisticsAllUser方法进入");
        //获取到所有用户  在这里做分页
        List<User> users = userMapper.selectAllUser(statisticsInfoParamPOJO.getPage(),statisticsInfoParamPOJO.getSize());

        //获取到有多少条数据
        Integer userCount = users.size();
        //封装结果信息,属性为用户Id,用户昵称,效绩信息
        List<StatisticsReturnPOJO> list = new ArrayList<>();


        //如果不传入时间
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            //设置搜索初始时间为当月一号
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));
            //设置搜索结束时间为此时此刻
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.now());
        }

        //开始统计
        for (User user : users) {
            //线索条数
            Integer resourceCount = resourceMapper.statisticsResourceCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //接待条数
            Integer interviewCount = interviewMapper.statisticsInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //有效接待条数
            Integer validInterviewCount = interviewMapper.statisticsValidInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //试听条数
            Integer tryCount = tryMapper.statisticsTryCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //回访条数
            Integer backviewCount = backviewMapper.statisticsBackviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //报名条数
            Integer baomingCount = baomingMapper.statisticsBaomingCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //预约回访条数
            Integer reservationBackviewCount = reservationBackviewMapper.statisticsReservationBackviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //预约接待条数
            Integer reservationInterviewCount = reservationInterviewMapper.statisticsReservationInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //预约试听条数
            Integer reservationTryCount = reservationTryMapper.statisticsReservationTry(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
            //封装
            StatisticsInfoPOJO statisticsInfoPOJO = new StatisticsInfoPOJO();
            StatisticsReturnPOJO statisticsReturnPOJO = new StatisticsReturnPOJO();
            statisticsInfoPOJO.setResourceCount(resourceCount);
            statisticsInfoPOJO.setInterviewCount(interviewCount);
            statisticsInfoPOJO.setValidInterviewCount(validInterviewCount);
            statisticsInfoPOJO.setTryCount(tryCount);
            statisticsInfoPOJO.setBackviewCount(backviewCount);
            statisticsInfoPOJO.setBaomingCount(baomingCount);
            statisticsInfoPOJO.setReservationBackviewCount(reservationBackviewCount);
            statisticsInfoPOJO.setReservationInterviewCount(reservationInterviewCount);
            statisticsInfoPOJO.setReservationTryCount(reservationTryCount);

            statisticsReturnPOJO.setStatisticsInfoPOJO(statisticsInfoPOJO);
            statisticsReturnPOJO.setUserNickname(user.getNickName());
            statisticsReturnPOJO.setUserId(user.getUserId());

            list.add(statisticsReturnPOJO);
        }
        //分页
        StatisticsPagePOJO statisticsPagePOJO = new StatisticsPagePOJO();
        statisticsPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
        statisticsPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
        statisticsPagePOJO.setTotoalCount(userCount);

        //计算总页数，将double转化成int
        Double doubleCount = (double) userCount / statisticsInfoParamPOJO.getSize();
        double ceil = Math.ceil(doubleCount);
        int totolPage = (int) ceil;
        statisticsPagePOJO.setTotalPage(totolPage);
        statisticsPagePOJO.setStatisticsReturnPOJOList(list);

        return ResultInfoV110.success(statisticsPagePOJO);
    }

    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 只统计自己的用户效绩信息
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsMyself(StatisticsInfoParamPOJO statisticsInfoParamPOJO,Integer userId) {
        log.info("StatisticsInfoServiceImpl的statisticsMyself方法进入");
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        User user = userMapper.selectOne(queryWrapper);

        //如果不传入时间
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            //设置搜索初始时间为当月一号
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));
            //设置搜索结束时间为此时此刻
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.now());
        }

        //线索条数
        Integer resourceCount = resourceMapper.statisticsResourceCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //接待条数
        Integer interviewCount = interviewMapper.statisticsInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //有效接待条数
        Integer validInterviewCount = interviewMapper.statisticsValidInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //试听条数
        Integer tryCount = tryMapper.statisticsTryCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //回访条数
        Integer backviewCount = backviewMapper.statisticsBackviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //报名条数
        Integer baomingCount = baomingMapper.statisticsBaomingCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //预约回访条数
        Integer reservationBackviewCount = reservationBackviewMapper.statisticsReservationBackviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //预约接待条数
        Integer reservationInterviewCount = reservationInterviewMapper.statisticsReservationInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //预约试听条数
        Integer reservationTryCount = reservationTryMapper.statisticsReservationTry(statisticsInfoParamPOJO.getSearchStartTime(),statisticsInfoParamPOJO.getSearchEndTime(),user.getUserId());
        //封装
        StatisticsInfoPOJO statisticsInfoPOJO = new StatisticsInfoPOJO();
        StatisticsReturnPOJO statisticsReturnPOJO = new StatisticsReturnPOJO();
        statisticsInfoPOJO.setResourceCount(resourceCount);
        statisticsInfoPOJO.setInterviewCount(interviewCount);
        statisticsInfoPOJO.setValidInterviewCount(validInterviewCount);
        statisticsInfoPOJO.setTryCount(tryCount);
        statisticsInfoPOJO.setBackviewCount(backviewCount);
        statisticsInfoPOJO.setBaomingCount(baomingCount);
        statisticsInfoPOJO.setReservationBackviewCount(reservationBackviewCount);
        statisticsInfoPOJO.setReservationInterviewCount(reservationInterviewCount);
        statisticsInfoPOJO.setReservationTryCount(reservationTryCount);

        statisticsReturnPOJO.setStatisticsInfoPOJO(statisticsInfoPOJO);
        statisticsReturnPOJO.setUserNickname(user.getNickName());
        statisticsReturnPOJO.setUserId(user.getUserId());

        return ResultInfoV110.success(statisticsReturnPOJO);
    }


    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计用户的所有未回访记录
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsAllUnHandleBackview(StatisticsInfoParamPOJO statisticsInfoParamPOJO) {
        log.info("StatisticsInfoServiceImpl的statisticsAllUnHandleBackview方法进入,参数statisticsInfoParamPOJO为" + statisticsInfoParamPOJO);
        //封装信息
        UnHandleReturnPagePOJO unHandleReturnPagePOJO = new UnHandleReturnPagePOJO();

        Integer page =  statisticsInfoParamPOJO.getPage();
        Integer size =  statisticsInfoParamPOJO.getSize();

        //如果不传入时间
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            //设置搜索初始时间为当月一号
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));
            //设置搜索结束时间为此时此刻
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.now());
        }

       //获取所有未回访记录
        List<BackviewAndNamePOJO> backviewAndNamePOJOList = backviewMapper.selectBackviewUnHandle(statisticsInfoParamPOJO.getSearchStartTime(),
                    statisticsInfoParamPOJO.getSearchEndTime(),null,page,size);



        //查询一共多少条数据
        Integer totalCount = backviewMapper.selectBackviewUnHandleCount(
                                                                statisticsInfoParamPOJO.getSearchStartTime(),
                                                                statisticsInfoParamPOJO.getSearchEndTime(),
                                                                null);


        //封装信息
        unHandleReturnPagePOJO.setBackviewAndNamePOJOList(backviewAndNamePOJOList);


        //封装分页信息
        unHandleReturnPagePOJO.setPage(page);
        unHandleReturnPagePOJO.setSize(size);
        unHandleReturnPagePOJO.setTotalCount(totalCount);
        //计算总页数，将double转化成int
        Double doubleCount = (double) totalCount/size;
        double ceil = Math.ceil(doubleCount);
        int totolPage = (int) ceil;
        unHandleReturnPagePOJO.setTotalPage(totolPage);

        return ResultInfoV110.success(unHandleReturnPagePOJO);
    }


    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 查看用户自己的所有未回访记录
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsMyHandleUnHandleBackview(StatisticsInfoParamPOJO statisticsInfoParamPOJO,Integer userId) {
        log.info("StatisticsInfoServiceImpl的statisticsMyHandleUnHandleBackview方法进入," +
                "参数statisticsInfoParamPOJO为"+statisticsInfoParamPOJO+"参数userId为"+userId);

        //封装信息
        UnHandleReturnPagePOJO unHandleReturnPagePOJO = new UnHandleReturnPagePOJO();

        //如果不传入时间
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            //设置搜索初始时间为当月一号
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));
            //设置搜索结束时间为此时此刻
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.now());
        }

        //获取自己的未回访记录   分页!
        List<BackviewAndNamePOJO> backviewAndNamePOJOList = backviewMapper.selectBackviewUnHandle(
                statisticsInfoParamPOJO.getSearchStartTime(),
                statisticsInfoParamPOJO.getSearchEndTime(),
                userId,
                statisticsInfoParamPOJO.getPage(),
                statisticsInfoParamPOJO.getSize());


        //获取自己的未回访记录 数量
        Integer totalCount = backviewMapper.selectBackviewUnHandleCount(statisticsInfoParamPOJO.getSearchStartTime(),
                                                                        statisticsInfoParamPOJO.getSearchEndTime(),
                                                                         userId);


        //封装信息!
        unHandleReturnPagePOJO.setBackviewAndNamePOJOList(backviewAndNamePOJOList);

        //封装分页信息
        unHandleReturnPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
        unHandleReturnPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
        unHandleReturnPagePOJO.setTotalCount(totalCount);
        unHandleReturnPagePOJO.setTotalPage(PageUtil.getTotalPage(totalCount,statisticsInfoParamPOJO.getSize()));

        //封装信息 值返回它自己的未回访记录即可
        return ResultInfoV110.success(unHandleReturnPagePOJO);
    }


    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计今日待办,待回访,未回访
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsTodayUnHandle(Integer autorId) {
       log.info("StatisticsInfoServiceImpl的statisticsTodayUnHandle方法进入,参数autorId为"+autorId);

        //回访差集长度即为今日待办数量
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //统计今日的预约回访学生Id
        List<Integer> statisticsTodayUnHandleBackviewStudentIdList = reservationBackviewMapper.statisticsTodayUnHandleBackviewStudentIdList(
                                                                                LocalDateTime.of(now.toLocalDate(), LocalTime.MIN),      //今日0时
                                                                                LocalDateTime.of(now.toLocalDate(), LocalTime.MAX),         //今日凌晨
                                                                                autorId);

        //统计今日的预约回访学生Id去重
        Set<Integer> reservationBackviewStudentIdSet = new HashSet<>(statisticsTodayUnHandleBackviewStudentIdList);

        //查询出这个用户的今日回访学生Id
        List<Integer> backviewStudentIdList = reservationBackviewMapper.statisticsTodayBackviewStudentIdList(
                                                                            LocalDateTime.of(now.toLocalDate(), LocalTime.MIN),
                                                                             LocalDateTime.of(now.toLocalDate(), LocalTime.MAX),
                                                                            autorId);
        //统计今日回访学生Id去重
        Set<Integer> backviewStudentIdSet = new HashSet<>(backviewStudentIdList);

        //差集
        Set<Integer> unHandleBackviewstudentIdResultSet = new HashSet<>();
        unHandleBackviewstudentIdResultSet.clear();
        unHandleBackviewstudentIdResultSet.addAll(reservationBackviewStudentIdSet);
        unHandleBackviewstudentIdResultSet.removeAll(backviewStudentIdSet);

        //获取到差集长度,即为今日未回访数量
        Integer unHandleBackviewCount = unHandleBackviewstudentIdResultSet.size();

        //到访差集长度即为今日待办数量
        //统计今日的预约到访学生id
        List<Integer> unHandleInterviewStudentIdList = reservationInterviewMapper.statisticsUnHandleInterviewStudentIdList(
                LocalDateTime.of(now.toLocalDate(), LocalTime.MIN),
                LocalDateTime.of(now.toLocalDate(), LocalTime.MAX),
                autorId);

        //统计今日的预约到访学生Id去重
        Set<Integer> unHandleInterviewStudentIdSet = new HashSet<>(unHandleInterviewStudentIdList);


        //统计今日的预约到访学生Id
        List<Integer> handleInterviewStudentIdList = reservationInterviewMapper.statisticsInterviewStudentIdList(
                LocalDateTime.of(now.toLocalDate(), LocalTime.MIN),
                LocalDateTime.of(now.toLocalDate(), LocalTime.MAX),
                autorId);

        //统计今日回访学生Id去重
        Set<Integer> handleInterviewStudentIdSet = new HashSet<>(handleInterviewStudentIdList);

        //差集
        Set<Integer> unHandleInterviewCountSet = new HashSet<>();
        unHandleInterviewCountSet.clear();
        unHandleInterviewCountSet.addAll(unHandleInterviewStudentIdSet);
        unHandleInterviewCountSet.removeAll(handleInterviewStudentIdSet);

        Integer unhandleInterviewTodayCount = unHandleInterviewCountSet.size();


        //封装信息
        Map<String,Integer> todayUnHandleItem = new HashMap<>();

        todayUnHandleItem.put("unHandleInterviewToday", unHandleBackviewCount);
        todayUnHandleItem.put("unHandleBackviewToday", unhandleInterviewTodayCount);

        return ResultInfoV110.success(todayUnHandleItem);

    }

    @Override
    public ResultInfoV110 statisticsAllTodayUnHandle() {

        //参数没有分页信息,搜索时间也为空,默认今天
        StatisticsInfoParamPOJO statisticsInfoParamPOJO = new StatisticsInfoParamPOJO();

        //调用上面的方法获取差集的长度 539,获取今日所有(今日)预约回访减去回访
        ResultInfoV110 unHandleBackviewAllTodayResultInfoV110 = this.statisticsUnHandleBackviewAllToday(statisticsInfoParamPOJO);

       //类型强转将获取到的ResultInfo转化成UnHandleBackviewAllTodayPagePOJO
        UnHandleBackviewAllTodayPagePOJO unHandleBackviewAllTodayPagePOJO = (UnHandleBackviewAllTodayPagePOJO) unHandleBackviewAllTodayResultInfoV110.get("DATA");
        //获取到总条数

        //调用上面的方法获取差集的长度 670
        ResultInfoV110 unHandleInterviewAllTodayResultInfoV110 = this.statisticsUnHandleInterviewAllToday(statisticsInfoParamPOJO);
        //类型强转将获取到的ResultInfo转化成UnHandleInterviewAllTodayPagePOJO
        UnHandleInterviewAllTodayPagePOJO unHandleInterviewAllTodayPagePOJO = (UnHandleInterviewAllTodayPagePOJO) unHandleInterviewAllTodayResultInfoV110.get("DATA");

        //统计市场人员今日总待办
        Map<String,Integer> todayAllUnHandle = new HashMap<>();

        todayAllUnHandle.put("unHandleAllInterviewToday",unHandleInterviewAllTodayPagePOJO.getTotalCount());
        todayAllUnHandle.put("unHandleAllBackviewToday", unHandleBackviewAllTodayPagePOJO.getTotalCount());

        return ResultInfoV110.success(todayAllUnHandle);
    }


    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计我自己的今日预约回访差集今日回访,按照今日的学生id去重求差集
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsUnHandleBackviewOfMyselfToday(StatisticsInfoParamPOJO statisticsInfoParamPOJO,
                                                                  Integer autorId) {

        log.info("StatisticsInfoServiceImpl的statisticsUnHandleBackviewOfMyselfToday方法进入,参数statisticsInfoParamPOJO为"
        +statisticsInfoParamPOJO+",参数autorId为"+autorId);

        //当前时间
        LocalDateTime now = LocalDateTime.now();

        //默认时间为今天 ,搜索时间可以修改
        if(statisticsInfoParamPOJO.getSearchEndTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MIN));
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MAX));
        }

        //统计今日的预约回访学生Id
        List<Integer> statisticsTodayUnHandleBackviewStudentIdList = reservationBackviewMapper.statisticsTodayUnHandleBackviewStudentIdList(
                                                                                                statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                                statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                                autorId);

        //统计今日的预约回访学生Id去重
        Set<Integer> reservationBackviewStudentIdSet = new HashSet<>(statisticsTodayUnHandleBackviewStudentIdList);

        //查询出这个用户的今日回访学生Id
        List<Integer> backviewStudentIdList = reservationBackviewMapper.statisticsTodayBackviewStudentIdList(statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                                             statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                                             autorId);
        //统计今日回访学生Id去重
        Set<Integer> backviewStudentIdSet = new HashSet<>(backviewStudentIdList);

        //差集
        Set<Integer> studentIdSet = new HashSet<>();
        studentIdSet.clear();
        studentIdSet.addAll(reservationBackviewStudentIdSet);
        studentIdSet.removeAll(backviewStudentIdList);

        //封装分页信息
        UnHandleBackviewAllTodayPagePOJO unHandleBackviewAllTodayPagePOJO
                = new UnHandleBackviewAllTodayPagePOJO();

        //如果今日有预约回访
        if(studentIdSet.size() != 0){
            //差集今日我自己的预约回访数量
            List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOList = reservationBackviewMapper.statisticsTodayUnHandleBackviewList(
                    statisticsInfoParamPOJO.getPage(),
                    statisticsInfoParamPOJO.getSize(),
                    statisticsInfoParamPOJO.getSearchStartTime(),
                    statisticsInfoParamPOJO.getSearchEndTime(),
                    studentIdSet,
                    autorId
            );

            //差集今日我自己的预约回访数量
            Integer todayUnHandleBackviewCount = reservationBackviewMapper.statisticsTodayUnHandleBackviewCount(statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                                statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                                studentIdSet,
                                                                                                autorId);

            unHandleBackviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
            unHandleBackviewAllTodayPagePOJO.setTotalCount(todayUnHandleBackviewCount);
            unHandleBackviewAllTodayPagePOJO.setTotalPage(PageUtil.getTotalPage(todayUnHandleBackviewCount,statisticsInfoParamPOJO.getSize()));
            unHandleBackviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOList);

            return  ResultInfoV110.success(unHandleBackviewAllTodayPagePOJO);
        }else{
            //如果今日没有预约回访
            List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOList = new ArrayList<>();
            unHandleBackviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setTotalPage(0);
            unHandleBackviewAllTodayPagePOJO.setTotalPage(0);
            unHandleBackviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOList);
            return ResultInfoV110.success(unHandleBackviewAllTodayPagePOJO);
        }
    }

    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计我自己的今日预约到访差集今日到访,按照今日的学生id去重求差集
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsUnHandleInterviewOfMyselfToday(StatisticsInfoParamPOJO statisticsInfoParamPOJO, Integer autorId) {
       log.info("StatisticsInfoServiceImpl的statisticsUnHandleInterviewOfMyselfToday方法进入,参数statisticsInfoParamPOJO为"+statisticsInfoParamPOJO+
               ",参数autorId为"+autorId);

        //当前时间
        LocalDateTime now = LocalDateTime.now();

        //默认时间为今天 ,搜索时间可以修改
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MIN));
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MAX));
        }

        //统计今日的预约到访学生id
        List<Integer> unHandleInterviewStudentIdList = reservationInterviewMapper.statisticsUnHandleInterviewStudentIdList(
                                                                                    statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                    statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                    autorId);

        //统计今日的预约到访学生Id去重
        Set<Integer> unHandleInterviewStudentIdSet = new HashSet<>(unHandleInterviewStudentIdList);


        //统计今日的预约到访学生Id
        List<Integer> handleInterviewStudentIdList = reservationInterviewMapper.statisticsInterviewStudentIdList(
                                                                                    statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                    statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                    autorId);

        //统计今日回访学生Id去重
        Set<Integer> handleInterviewStudentIdSet = new HashSet<>(handleInterviewStudentIdList);

        //差集
        Set<Integer> studentIdSet = new HashSet<>();
        studentIdSet.clear();
        studentIdSet.addAll(unHandleInterviewStudentIdSet);
        studentIdSet.removeAll(handleInterviewStudentIdSet);

        //封装分页信息
        UnHandleBackviewAllTodayPagePOJO unHandleBackviewAllTodayPagePOJO
                = new UnHandleBackviewAllTodayPagePOJO();


        //如果今日有预约到访
        if(studentIdSet.size() != 0){
            //查询我自己的今日未到访
            List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOList = reservationInterviewMapper.statisticsTodayUnHandleInterviewList(
                    statisticsInfoParamPOJO.getPage(),
                    statisticsInfoParamPOJO.getSize(),
                    statisticsInfoParamPOJO.getSearchStartTime(),
                    statisticsInfoParamPOJO.getSearchEndTime(),
                    studentIdSet,
                    autorId);

            //查询我自己的今日未到访数量
            Integer unHandleInterviewCount = reservationInterviewMapper.statisticsTodayUnHandleInterviewCount(statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                                statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                                studentIdSet,
                                                                                                autorId);

            unHandleBackviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
            unHandleBackviewAllTodayPagePOJO.setTotalCount(unHandleInterviewCount);
            unHandleBackviewAllTodayPagePOJO.setTotalPage(PageUtil.getTotalPage(unHandleInterviewCount,statisticsInfoParamPOJO.getSize()));
            unHandleBackviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOList);

            return ResultInfoV110.success(unHandleBackviewAllTodayPagePOJO);
        }
        else{//如果今日没有预约到访
            List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOList = new ArrayList<>();
            unHandleBackviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setTotalPage(0);
            unHandleBackviewAllTodayPagePOJO.setTotalPage(0);
            unHandleBackviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOList);
            return ResultInfoV110.success(unHandleBackviewAllTodayPagePOJO);
        }



    }


    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计所有的今日预约回访差集今日回访,按照今日的学生id去重求差集
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsUnHandleBackviewAllToday(StatisticsInfoParamPOJO statisticsInfoParamPOJO) {
        log.info("StatisticsInfoServiceImpl的statisticsUnHandleBackviewAllToday方法进入,参数statisticsInfoParamPOJO为"+statisticsInfoParamPOJO);

        //当前时间
        LocalDateTime now = LocalDateTime.now();

        //默认时间为今天 ,搜索时间可以修改
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MIN));
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MAX));
        }

        //查询出所有用户
        List<User> users = userMapper.selectAllUser(null,null);
        //所有市场用户
        List<User> marketUsers = new ArrayList<>();
        //if(user有市场管理权限的话)    根据user_id查询role_ids   根据roleIds查询permissions 是否有市场管理
        //userIds集合
        //遍历users
        //判断这个用户是否有"市场管理权限"
        for (User user : users) { //for outside

            Integer marketPermissionCount = 0;
            //根据userId获取对应的roleIds
            List<Integer> roleIdList = userRoleMapper.selectRoleIds(user.getUserId());
            //如果用户没有角色就跳出本次循环
            if(roleIdList.size() == 0){
                continue;
            }
            //根据rouIdList获取对应的PermissonIds
            List<Integer> permissionIds = rolePermissionMapper.selectUserPermission(roleIdList);
            //如果角色没有权限
            if(permissionIds.size() == 0){
                continue;
            }
            //遍历permissions查看是否有"市场管理权限"
            for (Integer permissionId : permissionIds) {

                if(permissionMapper.selectById(permissionId).getName().equals("咨询管理")){
                    marketPermissionCount++;
                }
            }
            //计数器 大于等于一的话 就是有"市场管理权限"
            if(marketPermissionCount >= 1){
                marketUsers.add(user);
            }
        } //for outside

        //遍历marketUsers取出所有有今日未回访的市场人员autorId
       List<Integer> autorIdList = new ArrayList<>();
        //查询出对应市场人员自己的今日未回访(今日(默认)预约回访减去今日(默认)回访)
        List<List<Integer>> studentIdListAll = new ArrayList<>();

        //遍历marketUsers求出每个autor的回访差集
        for (User marketUser : marketUsers) {
            //统计今日的预约回访学生Id
            List<Integer> statisticsTodayUnHandleBackviewStudentIdList = reservationBackviewMapper.statisticsTodayUnHandleBackviewStudentIdList(
                    statisticsInfoParamPOJO.getSearchStartTime(),
                    statisticsInfoParamPOJO.getSearchEndTime(),
                    marketUser.getUserId());

            //统计今日的预约回访学生Id去重
            Set<Integer> reservationBackviewStudentIdSet = new HashSet<>(statisticsTodayUnHandleBackviewStudentIdList);

            //查询出这个用户的今日回访学生Id
            List<Integer> backviewStudentIdList = reservationBackviewMapper.statisticsTodayBackviewStudentIdList(statisticsInfoParamPOJO.getSearchStartTime(),
                                                                                                                 statisticsInfoParamPOJO.getSearchEndTime(),
                                                                                                                  marketUser.getUserId());
            //统计今日回访学生Id去重
            Set<Integer> backviewStudentIdSet = new HashSet<>(backviewStudentIdList);

            //差集
            Set<Integer> studentIdSet = new HashSet<>();
            studentIdSet.clear();
            studentIdSet.addAll(reservationBackviewStudentIdSet);
            studentIdSet.removeAll(backviewStudentIdList);

            //将studentIdSet转化为List<Integer>
            List<Integer> studentIdList = new ArrayList<>(studentIdSet);

            //如果这个人今天没有未回访则不传入sql结果集合当中
            if(studentIdList.size() != 0){
                autorIdList.add(marketUser.getUserId());
                //将差集放入集合中
                studentIdListAll.add(studentIdList);
            }
        }


        System.out.println(autorIdList);
        System.out.println(studentIdListAll);
        //封装分页信息
        UnHandleBackviewAllTodayPagePOJO unHandleBackviewAllTodayPagePOJO
                = new UnHandleBackviewAllTodayPagePOJO();


        if(autorIdList.size() !=0){  //如果今天 哪怕只有一个人有待办事项则
            //获取所有今日(默认)的预约回访减去今日(默认)回访
            List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOS = reservationBackviewMapper.statisticsAllTodayUnHandleBackviewList(
                    statisticsInfoParamPOJO.getPage(),    //不设置为 null
                    statisticsInfoParamPOJO.getSize(),   //不设置为null
                    autorIdList,
                    studentIdListAll);

            Integer totalPage = 0;


            //统计查询条数
            Integer todayUnHandleBackviewCount =
                    reservationBackviewMapper.statisticsAllTodayUnHandleBackviewCount(autorIdList, studentIdListAll);


            //列表那边必须传page 和 size参数 !
            unHandleBackviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
            unHandleBackviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
            unHandleBackviewAllTodayPagePOJO.setTotalCount(todayUnHandleBackviewCount);
            //不传分页信息时默认传0
            if(statisticsInfoParamPOJO.getPage() != null && statisticsInfoParamPOJO.getSize() != null){
                totalPage =   PageUtil.getTotalPage(todayUnHandleBackviewCount,statisticsInfoParamPOJO.getSize());
            }else{
                totalPage = 0;
            }

            unHandleBackviewAllTodayPagePOJO.setTotalPage(totalPage);
            unHandleBackviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOS);
            return ResultInfoV110.success(unHandleBackviewAllTodayPagePOJO);
        }
        List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOS =new ArrayList<>();
        unHandleBackviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
        unHandleBackviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
        unHandleBackviewAllTodayPagePOJO.setTotalCount(0);
        unHandleBackviewAllTodayPagePOJO.setTotalPage(0);
        unHandleBackviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOS);

        return ResultInfoV110.success(unHandleBackviewAllTodayPagePOJO);

    }


    /**
     * @param
     * @return ResultInfoV110
     * @Author wuweinan
     * @Description 统计所有的今日预约到访差集今日回访,按照今日的学生id去重求差集,重复调用477行方法
     * @Date 2021-8-9
     */
    @Override
    public ResultInfoV110 statisticsUnHandleInterviewAllToday(StatisticsInfoParamPOJO statisticsInfoParamPOJO) {
        log.info("StatisticsInfoServiceImpl的statisticsUnHandleInterviewAllToday方法进入," +
                "参数statisticsInfoParamPOJO为"+statisticsInfoParamPOJO);

        //当前时间
        LocalDateTime now = LocalDateTime.now();

        //默认时间为今天 ,搜索时间可以修改
        if(statisticsInfoParamPOJO.getSearchStartTime() == null && statisticsInfoParamPOJO.getSearchEndTime() == null){
            statisticsInfoParamPOJO.setSearchStartTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MIN));
            statisticsInfoParamPOJO.setSearchEndTime(LocalDateTime.of(now.toLocalDate(), LocalTime.MAX));
        }

        //查询出所有市场人员
        //查询出所有用户
        List<User> users = userMapper.selectAllUser(null,null);
        //所有市场用户
        List<User> marketUsers = new ArrayList<>();
        //if(user有市场管理权限的话)    根据user_id查询role_ids   根据roleIds查询permissions 是否有市场管理
        //userIds集合
        //遍历users
        //判断这个用户是否有"市场管理权限"
        for (User user : users) { //for outside

            Integer marketPermissionCount = 0;
            //根据userId获取对应的roleIds
            List<Integer> roleIdList = userRoleMapper.selectRoleIds(user.getUserId());
            //如果用户没有角色就跳出本次循环
            if(roleIdList.size() == 0){
                continue;
            }
            //根据rouIdList获取对应的PermissonIds
            List<Integer> permissionIds = rolePermissionMapper.selectUserPermission(roleIdList);
            //如果角色没有权限
            if(permissionIds.size() == 0){
                continue;
            }
            //遍历permissions查看是否有"市场管理权限"
            for (Integer permissionId : permissionIds) {

                if(permissionMapper.selectById(permissionId).getName().equals("咨询管理")){
                    marketPermissionCount++;
                }
            }
            //计数器 大于等于一的话 就是有"市场管理权限"
            if(marketPermissionCount >= 1){
                marketUsers.add(user);
            }
        } //for outside
        //遍历marketUsers取出所有有今日未到访的市场人员autorId
        List<Integer> autorIdList = new ArrayList<>();

        //查询出对应市场人员自己的今日未回访(今日(默认)预约回访减去今日(默认)回访)
        List<List<Integer>> studentIdListAll = new ArrayList<>();

        //遍历marketUers求出每个autor的到访差集
        for (User marketUser : marketUsers) {

            //统计今日的预约到访学生id
            List<Integer> unHandleInterviewStudentIdList = reservationInterviewMapper.statisticsUnHandleInterviewStudentIdList(
                    statisticsInfoParamPOJO.getSearchStartTime(),
                    statisticsInfoParamPOJO.getSearchEndTime(),
                    marketUser.getUserId());

            //统计今日的预约到访学生Id去重
            Set<Integer> unHandleInterviewStudentIdSet = new HashSet<>(unHandleInterviewStudentIdList);
            //统计今日的预约到访学生Id
            List<Integer> handleInterviewStudentIdList = reservationInterviewMapper.statisticsInterviewStudentIdList(
                    statisticsInfoParamPOJO.getSearchStartTime(),
                    statisticsInfoParamPOJO.getSearchEndTime(),
                    marketUser.getUserId());

            //统计今日回访学生Id去重
            Set<Integer> handleInterviewStudentIdSet = new HashSet<>(handleInterviewStudentIdList);
            //差集
            Set<Integer> studentIdSet = new HashSet<>();
            studentIdSet.clear();
            studentIdSet.addAll(unHandleInterviewStudentIdSet);
            studentIdSet.removeAll(handleInterviewStudentIdSet);

            //将studentIdSet转化为List<Integer>
            List<Integer> studentIdList = new ArrayList<>(studentIdSet);

            //如果这个人今天没有为到访待办则不传入sql结果集合中
            if(studentIdList.size() != 0){
                autorIdList.add(marketUser.getUserId());
                //将差集放入集合中
                studentIdListAll.add(studentIdList);
            }
        }

        //封装分页信息
        UnHandleInterviewAllTodayPagePOJO unHandleInterviewAllTodayPagePOJO
                = new UnHandleInterviewAllTodayPagePOJO();

        if(autorIdList.size() != 0){
            //获取所有今日(默认)的预约到访减去今日(默认)回访
            List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOS = reservationInterviewMapper.statisticsAllTodayUnHandleInterviewList(
                    statisticsInfoParamPOJO.getPage(),
                    statisticsInfoParamPOJO.getSize(),
                    autorIdList,
                    studentIdListAll);

            Integer totalPage = 0;

            //获取所有今日(默认)的预约到访减去今日(默认)回访条数
            Integer  unHandleInterviewCount =
                    reservationInterviewMapper.statisticsAllTodayUnHandleInterviewCount(autorIdList, studentIdListAll);

            unHandleInterviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
            unHandleInterviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
            unHandleInterviewAllTodayPagePOJO.setTotalCount(unHandleInterviewCount);

            //不传分页信息时默认传0
            if(statisticsInfoParamPOJO.getPage() != null && statisticsInfoParamPOJO.getSize() != null){
                totalPage =   PageUtil.getTotalPage(unHandleInterviewCount,statisticsInfoParamPOJO.getSize());
            }else{
                totalPage = 0;
            }
            unHandleInterviewAllTodayPagePOJO.setTotalPage(totalPage);
            unHandleInterviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOS);
            return ResultInfoV110.success(unHandleInterviewAllTodayPagePOJO);
        }
        List<ReservationBackviewAndNamePOJO> reservationBackviewAndNamePOJOS = new ArrayList<>();
        unHandleInterviewAllTodayPagePOJO.setPage(statisticsInfoParamPOJO.getPage());
        unHandleInterviewAllTodayPagePOJO.setSize(statisticsInfoParamPOJO.getSize());
        unHandleInterviewAllTodayPagePOJO.setTotalCount(0);
        unHandleInterviewAllTodayPagePOJO.setTotalPage(0);
        unHandleInterviewAllTodayPagePOJO.setReservationBackviewAndNamePOJOS(reservationBackviewAndNamePOJOS);
        return ResultInfoV110.success(unHandleInterviewAllTodayPagePOJO);

    }
}
